TI_GDT	equ 000b
RPL0	equ 00b
SELECTOR_VIDEO equ (0x0003 << 3) + TI_GDT + RPL0

section .data
put_int_buffer	dq 	0	;define 8 byte buf to keep char of 4 byte hex number; 4 bit is a hex number,8 bit is a char

[bits 32]
section .text
;-----------------put string----------------------
;Description:put the string to screen
;-------------------------------------------------
global put_str			;C global value
put_str:
push ebx
push ecx
xor ecx,ecx
mov ebx,[esp + 12]		;get top of string addr,first char addr     stack top ecx ebx retAddr
.goon:				;inside symbol
mov cl,[ebx]			;from ebx get one char 1byte
cmp cl,0			;get 0 mean the end of string
jz .str_over
push ecx			;set parameter for put_char,push 4byte into stack translate 1byte
call put_char
add esp,4			;recycle stack memary 4byte
inc ebx				;add 1byte is next char
jmp .goon
.str_over:
pop ecx
pop ebx
ret

;-------------------put_char-----------------------
;Description:put the first char in stack to screen
;--------------------------------------------------
global put_char       	;asm global symbol can use to link process
put_char:
pushad			;backup all 32bits register
mov ax,SELECTOR_VIDEO
mov gs,ax 

;------------------get cursor postion---------------
mov dx,0x03d4		;index register       AVG standard
mov al,0x0e		;get heigh 8 bits of cursor
out dx,al		
mov dx,0x03d5		;read data port
in al,dx
mov ah,al

mov dx,0x03d4
mov al,0x0f
out dx,al
mov dx,0x03d5
in al,dx

mov bx,ax		;put cursor postion to bx

;-------------------get char from stack----------
mov ecx,[esp + 36]	;get char from stack
cmp cl,0x0d		;CR is 0x0d,LF is 0x0a,BS is 0x08
jz .is_carriage_return
cmp cl,0x0a
jz .is_line_feed
cmp cl,0x08
jz .is_backspace
jmp .put_other

;--------------------backspace--------------------
.is_backspace:
dec bx
shl bx,1
mov byte [gs:bx],0x20	;space is 0x20
inc bx
mov byte [gs:bx],0x07
shr bx,1
jmp .set_cursor		;bx only keep the value of cursor postion,need to put it back to AVG register

;--------------------other char---------------------
.put_other:
shl bx,1
mov [gs:bx],cl
inc bx
mov byte [gs:bx],0x07
shr bx,1
inc bx
cmp bx,2000
jl .set_cursor		;if less than 2000 then goto set cursor,if not goto LF

;--------------------CR LF--------------------------
.is_line_feed:
.is_carriage_return:
xor dx,dx		
mov ax,bx
mov si,80
div si
sub bx,dx

.is_carriage_return_end:
add bx,80
cmp bx,2000
.is_line_feed_end:
jl .set_cursor

;--------------------roll screen--------------------
.roll_screen:		;bx bigger than 2000,then roll screen
cld
mov ecx,960
mov esi,0xb80a0
mov edi,0xb8000
rep movsd

mov ebx,3840
mov ecx,80
.cls:
mov word [gs:ebx],0x0720	;0x20 is space,0x07 is backgroud
add ebx,2
loop .cls
mov bx,1920

;--------------------set cursor------------------------
.set_cursor:
mov dx,0x03d4
mov al,0x0e
out dx,al
mov dx,0x3d5
mov al,bh
out dx,al

mov dx,0x03d4
mov al,0x0f
out dx,al
mov dx,0x03d5
mov al,bl
out dx,al

.put_char_done:
popad
ret

;--------------------put_int-----------------------------
;input:   int number 4 byte in stack
;output:  put hex number in screen
;--------------------------------------------------------
global put_int
put_int:
pushad
mov ebp, esp
mov eax,[ebp + 4*9]
mov edx,eax		;get number
mov edi,7		;offset of buffer
mov ecx,8		
mov ebx,put_int_buffer	;addr of buffer

.16based_4bits:
and edx,0x0000000F
cmp edx,9
jg .is_A2F
add edx, '0'		; '0' ascii  00110000    '1' ascii 00110001  ......
jmp .store

.is_A2F:
sub edx,10
add edx,'A'

.store:
mov [ebx+edi],dl
dec edi
shr eax,4
mov edx,eax
loop .16based_4bits

.ready_to_print:
inc edi 		;edi=-1(0xffffffff),add 1 make it 0x00000000
.skip_prefix_0:
cmp edi,8
je .full0

.go_on_skip:
mov cl,[put_int_buffer+edi]
inc edi
cmp cl,'0'
je .skip_prefix_0
dec edi
jmp .put_each_num

.full0:
mov cl,'0'
.put_each_num:
push ecx
call put_char
add esp,4
inc edi
mov cl,[put_int_buffer+edi]
cmp edi,8
jl .put_each_num
popad
ret

global set_cursor
set_cursor:
pushad
mov bx,[esp+36]
;set high 8bits;
mov dx,0x03d4
mov al,0x0e
out dx,al
mov dx,0x03d5
mov al,bh
out dx,al

;set low 8bits;
mov dx,0x03d4
mov al,0x0f
out dx,al
mov dx,0x03d5
mov al,bl
out dx,al
popad
ret

 


















































































































